package com.nhim.cms.admin.server;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.nhim.cms.domain.ElementDefinition;
import com.nhim.cms.util.AppContext;
import com.nhim.cms.util.ElementDefinitionManager;

@Service("ElementDefinitionService")
public class ElementDefinitionService {
	
	static Logger logger = Logger
	.getLogger(ElementDefinitionService.class);
	public static Integer countElementDefinitionsBySearch(String startsWith) {
		return ElementDefinitionManager
		.getFromApplicationContext(AppContext.getApplicationContext()).countElementDefinitionsBySearch(startsWith);
	}

	public static List<ElementDefinition> findElementDefinitionEntriesBySearch(
			String startsWith, String orderBy) {
		return ElementDefinitionManager
		.getFromApplicationContext(AppContext.getApplicationContext()).findElementDefinitionEntriesBySearch(startsWith,
				orderBy);
	}
	public static ElementDefinition persit(ElementDefinition element)
	{
		try {
			return ElementDefinitionManager
			.getFromApplicationContext(AppContext.getApplicationContext()).saveOrUpdate(element);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
		
	}
	public static ElementDefinition findById(Integer id)
	{
		return ElementDefinitionManager
		.getFromApplicationContext(AppContext.getApplicationContext()).findById(id);
	}
	public static Boolean isNameExist(String name)
	{
		ElementDefinition result = ElementDefinitionManager
		.getFromApplicationContext(AppContext.getApplicationContext()).findByName(name);
		if(result != null)
			return true;
		return false;
	}
	public static Boolean remove(Integer id)
	{
		try {
			ElementDefinitionManager
			.getFromApplicationContext(AppContext.getApplicationContext()).delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
}
